package com.thl.levelcache.decoration.connection;


import com.thl.levelcache.decoration.up.RedisDecorate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author zhao yi
 * @create 2020-05-24 12:29
 * @desc
 **/
@Slf4j
@Component
public class RedisCheckConnection {

    @Autowired
    private RedisConnectionFactory connectionFactory;

    @Async
    public void ping() {
        RedisConnection connection = connectionFactory.getConnection();
        while (true) {
            try {
                String ping = connection.ping();
                Thread.sleep(5000);
                log.info("ping redis connection success !!! {} ", ping);
                RedisDecorate.IS_OPENG_REDIS = true;
                if (RedisDecorate.IS_OPENG_REDIS) {
                    break;
                }
            } catch (Exception e) {
                log.info("ping redis connection timeout... ");
                RedisDecorate.IS_OPENG_REDIS = false;
                e.printStackTrace();
            }

        }

    }

}
